package org.nanotek.tika.job.writer;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.tika.io.TemporaryResources;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.nanotek.Base;
import org.nanotek.tika.Detector;
import org.nanotek.tika.MediaDetail;
import org.nanotek.tika.io.TikaMediaDetector;
import org.nanotek.tika.job.reader.DetectionException;

public class TikaMediaDetailDetector<T extends MediaDetail , R extends Base<?>> implements Detector <T,R> {

	private TikaMediaDetector tikaMediaDetector; 


	@SuppressWarnings("unchecked")
	@Override
	public T detect(R ref)  throws DetectionException {
		MediaDetail mediaDetails = null;
		try {
			TemporaryResources tmp = new TemporaryResources();
			String fileLocation = (String) ref.getId();
			File refFile = new File(fileLocation); 
			if (!refFile.isDirectory()){ 
				TikaInputStream input = TikaInputStream.get(new FileInputStream(fileLocation), tmp);
				mediaDetails = tikaMediaDetector.detectMediaType(input);
				mediaDetails.setLocation(fileLocation);
			} 
		} catch (Exception ex) 
		{ 
			ex.printStackTrace();
//			throw new DetectionException(ex);
		}
		return  (T) mediaDetails;
	}


	public TikaMediaDetector getTikaMediaDetector() {
		return tikaMediaDetector;
	}


	public void setTikaMediaDetector(TikaMediaDetector tikaMediaDetector) {
		this.tikaMediaDetector = tikaMediaDetector;
	}

}
